<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script  runat="server">
Dim TotalRecords=0,TotalPages=0
Dim Records=5,CurrentPage=0
Sub Page_Load(ByVal sender As Object, _
	ByVal e As EventArgs)
	If Not Page.IsPostBack Then			
		BindGrid()
        	End If
	TotalPages=DataResult.PageCount	
End Sub
Sub MyDataGridPage(sender As Object, e As _
	DataGridPageChangedEventArgs) 		
	DataResult.CurrentPageIndex = e.NewPageIndex
	BindGrid	
End Sub
Private Sub BindGrid()		
	DataResult.PageSize=Records	
	CurrentPage=DataResult.CurrentPageIndex+1		 
	Dim strSQL = "select * from tblSubcategories"
	Dim myConnection As SqlConnection = _
	New SqlConnection(ConfigurationSettings.appSettings("conn"))	
	dim myAdapter as SqlDataAdapter = _
	New SqlDataAdapter(strSQL, myConnection)
	dim myDataSet as DataSet = New DataSet()		
	myAdapter.Fill(myDataSet,strSQL)
	TotalRecords=myDataSet.Tables(strSQL).Rows().Count	
	DataResult.DataSource=myDataSet
	DataResult.DataBind()
End Sub
Sub PagerButtonClick(sender As Object, e As EventArgs)        
        Dim arg As string = sender.CommandArgument
        Select arg		
            Case "next":
               If (DataResult.CurrentPageIndex < (DataResult.PageCount - 1)) Then
                    DataResult.CurrentPageIndex += 1
               End If    
            Case "prev":
                If (DataResult.CurrentPageIndex > 0) Then
                    DataResult.CurrentPageIndex -= 1
                End If
        Case "last":
                DataResult.CurrentPageIndex = (DataResult.PageCount - 1)
        Case Else:
                 DataResult.CurrentPageIndex = System.Convert.ToInt32(arg)
        End Select
    BindGrid
End Sub
</script>
<html>
<head>
<title>DataGrid Control</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>DataGrid and Paging</h2>
<form runat=server>
                   <table width="100%" border="0" cellpadding="0" cellspacing="0">
                      <tr> 
                        <td colspan=2>          
                          <asp:DataGrid  
		ID="DataResult"	
		ClientID="dgGrid"	
		AllowSorting="True"
		AllowPaging="True"
		AllowCustomPaging = "True"
		PagerStyle-PageButtonCount=2
		PagerStyle-Mode="NextPrev"						
		PagerStyle-NextPageText=""
		PagerStyle-PrevPageText=""
		OnPageIndexChanged="MyDataGridPage" 
		BorderColor="black"
		BorderWidth="0"
		GridLines="Both"
		CellPadding="3"
		CellSpacing="1"
		Font-Name="Verdana"
		Font-Size="8pt"
		width="100%" 														
		HeaderStyle-CssClass="row_top"
		Headerstyle-BackColor="gray"
		Headerstyle-Font-Name="Arial"
		Headerstyle-Font-Size="10"
	        	Headerstyle-Font-Bold="True" 
		AlternatingItemStyle-BackColor="#e5f4ff"
		AlternatingItemStyle-ForeColor="blue"
		AlternatingItemStyle-Font-Name="Arial"
		ShowHeader="True"
		ShowFooter="False"
		AutoGenerateColumns="False" runat="server" > <Columns> 
                          	<asp:TemplateColumn HeaderText="CateID"> <ItemTemplate> 
                          	<asp:Label ID="ItemID" Text=<%# DataBinder.Eval(Container.DataItem, "SubCateID").toString() %> Runat=server /> 
                          	</ItemTemplate> </asp:TemplateColumn>
		<asp:TemplateColumn HeaderText="CateName"> <ItemTemplate> 
                          	<asp:Label ID="ItemName" Text=<%# DataBinder.Eval(Container.DataItem, "SubCateName").toString() %> Runat=server /> 
                          	</ItemTemplate> </asp:TemplateColumn>
		</Columns> </asp:DataGrid> 
                        </td>
              </tr>	
	<tr><td>Current Page: <b><%=CurrentPage%></b>
	of <b><%=TotalPages%></b></td><td align=right>
	<%if TotalRecords > Records then%>
	<asp:LinkButton id="btnFirst" runat="server" 
	Text="First" 
	CommandArgument="0" 
	ForeColor="navy" 
	Font-Name="verdana" 
	Font-size="8pt" 
	OnClick="PagerButtonClick" />&nbsp;
	<asp:LinkButton id="btnPrev" runat="server" 
	Text="<img src=../../images/Back1.gif border=0>" 				
	CommandArgument="prev" 
	ForeColor="navy" 
	Font-Name="verdana" Font-size="8pt" 
	OnClick="PagerButtonClick"/>&nbsp;
	<asp:LinkButton id="btnNext" runat="server" 
	Text="<img src=../../images/Next1.gif border=0>" 
	CommandArgument="next" 
	ForeColor="navy" 
	Font-Name="verdana" Font-size="8pt" 
	OnClick="PagerButtonClick" />&nbsp;
	<asp:LinkButton id="btnLast" runat="server" 
	Text="Last" 
	CommandArgument="last" 
	ForeColor="navy" 
	Font-Name="verdana" Font-size="8pt" 
	OnClick="PagerButtonClick"/> 
              <%end if%> 
	</td></tr>
       </table>
</form>
</body>
</html>
